package saxion.speelveld.spaceengineer.database;

import saxion.speelveld.spaceengineer.model.Building;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class BuildingTypeDAO
{
	private SQLiteDatabase database;

	private BuildingTypeDB dbHelper;

	public BuildingTypeDAO(Context context)
	{
		dbHelper = new BuildingTypeDB(context);
	}

	/**
	 * Open the database connection.
	 * 
	 * @throws SQLException
	 */
	public void open() throws SQLException
	{
		database = dbHelper.getWritableDatabase();
	}

	/**
	 * Close the database connection.
	 */
	public void close()
	{
		dbHelper.close();
	}

	public void setInitialBuildingTypes()
	{
		if (hasRows() == false)
		{
			try
			{
				// Log.i("BuildingTypeDAO.setInitialBuildingTypes", "putting em all in da DB.");
				database.beginTransaction();
				// Log.i("BuildingTypeDAO.setInitialBuildingTypes", "starting transaction. ("
				// + Building.GetAllDifferentBuildings().size() + ")");
				for (Building building : Building.GetAllDifferentBuildings())
				{
					// Log.d("BuildingTypeDAO.setInitialBuildingTypes", "putting em all in da DB :)");
					// database.execSQL("INSERT INTO " + BuildingTypeDB.TABLE_NAME +
					// " (" + BuildingTypeDB.COLUMN_BUILDINGNAME + ") "
					// + " VALUES (" + building.getBuildingName() + ");");
					ContentValues values = new ContentValues();
					values.put(BuildingTypeDB.COLUMN_BUILDINGNAME, building.getBuildingName());
					// Log.i("setInitialBuildingTypes", building.getBuildingName());
					database.insert(BuildingTypeDB.TABLE_NAME, null, values);
				}
				database.setTransactionSuccessful();
			}
			finally
			{
				database.endTransaction();
				// Log.i("BuildingTypeDAO.setInitialBuildingTypes", "ending transaction.");
			}
		}
	}

	public boolean hasRows()
	{
		Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM " + BuildingTypeDB.TABLE_NAME, null);
		cursor.moveToFirst();
		int rowCount = cursor.getInt(0);

		// Log.d("BuildingTypeDAO.setInitialBuildingTypes", "rows = " + rowCount);

		cursor.close();
		if (rowCount == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	public int GetTypeIdByName(String buildingName)
	{
		Log.d("BuildingTypeDAO.getTypeIdByName", buildingName);
		String sql = "SELECT * FROM " + BuildingTypeDB.TABLE_NAME + " WHERE " +
			BuildingTypeDB.COLUMN_BUILDINGNAME + " = \"" + buildingName + "\"";
		Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();

		int typeId = cursor.getInt(cursor.getColumnIndex(BuildingTypeDB.COLUMN_ID));
		cursor.close();
		return typeId;
	}

	public String getTypeNameById(int buildingTypeId)
	{
		String sql = "SELECT * FROM " + BuildingTypeDB.TABLE_NAME + " WHERE " +
			BuildingTypeDB.COLUMN_ID + " = " + buildingTypeId;
		Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();

		String typeName = cursor.getString(cursor.getColumnIndex(BuildingTypeDB.COLUMN_BUILDINGNAME));
		cursor.close();

		return typeName;
	}
}
